#!/usr/bin/env  python
# -*- coding: cp936 -*-
# Brief:        python file
# Author:       Aragon<zmule@163.com>
# Last Change:  2007-08-29 14:11:41
# Version:      0.1

import yaml
import wordnet
import wntools 
from operator import itemgetter

class Classify:
    def __init__(self):
    #read weight.yaml
        file = open("webcount.yaml", 'r')
        self.files = yaml.load(file)
        if not self.files:
            self.files = {}
        file.close()
        self.classify = {}

        webDict = {}
        for file, termDict in self.files.items():
            webDict[file] = {}
            d = webDict[file]
            for k, v in termDict.items():
                webCount = v[0]
                if webCount in d:
                    d[webCount].append(k)
                else:
                    d[webCount] = []
                    d[webCount].append(k)
            terms = []
            for v in d.values():
                for t in v:
                    terms.append(t)
            l = len(terms)
            terms = terms[l-2:l]
            webDict[file] = terms
        file = open('web.yaml', 'w+')
        yaml.dump(webDict, file)
        file.close()
        self.webDict = webDict

    def meetTerms(self):
        for file, terms in self.webDict.items():
            self.__meet2(file, terms[0], terms[1])

    def __meet2(self, file, term1, term2):
        try:
            t1Sensor = wordnet.N[term1][0]
            t2Sensor = wordnet.N[term2][0]
            
            t1Member = t1Sensor.pointerTargets(wordnet.MEMBER_MERONYM)
            t2Member = t2Sensor.pointerTargets(wordnet.MEMBER_MERONYM)
            t12meet =  wntools.meet(t1Sensor, t2Sensor)
            print "%s  %s:%s, %s:%s, meet:%s" %(file, term1,t1Member, term2, t2Member, t12meet[0])
#            self.classify[file] = t2Member
#            self.classify[file] = str(t12meet[0]).splite()[0]
        except:
            self.classify[file] = term2

    def save(self):
        file = open("classify.yaml", 'w+')
        yaml.dump(self.classify, file)
        file.close()

if __name__ == '__main__':
    classify = Classify()
    classify.meetTerms()
    classify.save()


